Vue.component('Add', {
    template: `
		<el-dialog title="添加矿机" width="900px" class="icon-dialog" :visible.sync="show" @open="open" :before-close="closeForm" append-to-body>
			<el-form :size="size" ref="form" :model="form" :rules="rules" :label-width=" ismobile()?'90px':'16%'">
                <el-row >
                  <el-col :span="24">
                    <el-form-item label="所属矿机" prop="linkcata_id">
                      <el-select   style="width:100%" v-model="form.pid" filterable clearable placeholder="请选择所属矿机">
                        <el-option v-for="(item,i) in pinfo" :key="i" :label="item.name" :value="item.id"></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row >
                <el-col :span="24">
                  <el-form-item label="套餐名称" prop="name">
                    <el-input  v-model="form.name" autoComplete="off" clearable placeholder="简体"></el-input>

                    <el-input  v-model="form.t_name" autoComplete="off" clearable placeholder="繁体"></el-input>

                    <el-input  v-model="form.e_name" autoComplete="off" clearable placeholder="英文"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row >
                <el-col :span="24">
                  <el-form-item label="标签名称" prop="label">
                    <el-input  v-model="form.label" autoComplete="off" clearable placeholder="简体"></el-input>

                    <el-input  v-model="form.t_label" autoComplete="off" clearable placeholder="繁体"></el-input>

                    <el-input  v-model="form.e_label" autoComplete="off" clearable placeholder="英文"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row >
                <el-col :span="24">
                  <el-form-item label="矿机图标" prop="pic">
                    <Upload v-if="show" size="small"  file_type="image" filterable :image.sync="form.pic"></Upload>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row >
                <el-col :span="24">
                  <el-form-item label="矿机图标(繁体)" prop="e_pic">
                    <Upload v-if="show" size="small"  file_type="image" filterable :image.sync="form.e_pic"></Upload>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row >
                <el-col :span="24">
                  <el-form-item label="矿机图标(英文)" prop="t_pic">
                    <Upload v-if="show" size="small"  file_type="image" filterable :image.sync="form.t_pic"></Upload>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="24" >
                  <el-form-item label="购买单价" prop="price">
                    <el-input typt="number" v-model="form.money" autoComplete="off" filterable clearable placeholder="请输入价格"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="24" >
                  <el-form-item label="个人最大持有量" prop="max_possess">
                    <el-input typt="number" v-model="form.max_possess" autoComplete="off" filterable clearable placeholder="请输入个人最大持有量 0:为不限制"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row >
                <el-col :span="24">
                  <el-form-item label="有效日期(天)" prop="duration">
                    <el-input typt="number" v-model="form.duration" autoComplete="off" filterable clearable placeholder="请输入天数"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row >
                <el-col :span="24">
                  <el-form-item label="每日释放数量" prop="daily_release">
                    <el-input typt="number" v-model="form.daily_release" autoComplete="off" filterable clearable placeholder="请输入数量"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row >
                <el-col :span="24">
                  <el-form-item label="矿机状态:">
                    <el-radio-group v-model="form.status">
                      <el-radio :label="1">出售中</el-radio>
                      <el-radio :label="2">暂停</el-radio>
                      <el-radio :label="3">已释放完毕</el-radio>
                    </el-radio-group>
                  </el-form-item>
                </el-col>
              </el-row>
              
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button :size="size" :loading="loading" type="primary" @click="submit" >
					<span v-if="!loading">确 定</span>
					<span v-else>提 交 中...</span>
				</el-button>
				<el-button :size="size" @click="closeForm">取 消</el-button>
			</div>
		</el-dialog>
	`,
    components:{
    },
    props: {
        show: {
            type: Boolean,
            default: false
        },
        size: {
            type: String,
            default: 'small'
        },
    },
    watch:{
        show(val){
            if(val){
                axios.post(base_url + '/Mining/getList').then(res => {
                    if(res.data.status == 200){
                        this.pinfo = res.data.data
                    }
                })
            }
        }
    },
    data(){
        return {
            form: {
                pid:'请选择所属矿机',
                name:'',
                e_name:'',
                t_name:'',
                label:'',
                e_label:'',
                t_label:'',
                pic:'',
                e_pic:'',
                t_pic:'',
                money:'',
                max_possess:0,
                duration:'',
                daily_release:0,
                status:1,
            },
            pinfo:[],
            loading:false,
            rules: {
                name:[
                    {required: true, message: '矿机名称不能为空', trigger: 'blur'},
                ],
                e_name:[
                    {required: true, message: '矿机名称不能为空', trigger: 'blur'},
                ],
                t_name:[
                    {required: true, message: '矿机名称不能为空', trigger: 'blur'},
                ],
                label:[
                    {required: true, message: '标签不能为空', trigger: 'blur'},
                ],
                e_label:[
                    {required: true, message: '标签不能为空', trigger: 'blur'},
                ],
                t_label:[
                    {required: true, message: '标签不能为空', trigger: 'blur'},
                ],
                pic:[
                    {required: true, message: '请上传矿机图标', trigger: 'blur'},
                ],
                e_pic:[
                    {required: true, message: '请上传矿机图标', trigger: 'blur'},
                ],
                t_pic:[
                    {required: true, message: '请上传矿机图标', trigger: 'blur'},
                ],
                money:[
                    {required: true, message: '请输入套餐单价', trigger: 'blur'},
                ],
                duration:[
                    {required: true, message: '请输入套餐有效日期', trigger: 'blur'},
                ],
                daily_release:[
                    {required: true, message: '请输入套餐每日释放数量', trigger: 'blur'},
                ],
            }
        }
    },

    methods: {
        open(){
            // this.form.pid = this.info.pid
            console.log(this.info)
        },
        submit(){
            this.$refs['form'].validate(valid => {
                if(valid) {
                    this.loading = true
                    axios.post(base_url + '/MiningSpecification/add',this.form).then(res => {
                        if(res.data.status == 200){
                            this.$message({message: res.data.msg, type: 'success'})
                            this.$emit('refesh_list')
                            this.closeForm()
                        }else{
                            this.loading = false
                            this.$message.error(res.data.msg)
                        }
                    }).catch(()=>{
                        this.loading = false
                    })
                }
            })
        },
        closeForm(){
            this.$emit('update:show', false)
            this.loading = false
            if (this.$refs['form']!==undefined) {
                this.$refs['form'].resetFields()
            }
        },
    }
})
